package com.able.wanandroid.module.hotkey.repository.remote;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;

import com.able.wanandroid.bean.BaseResult;
import com.able.wanandroid.bean.HotKeyBean;
import com.able.wanandroid.bean.Resource;
import com.able.wanandroid.http.ApiCallback;
import com.able.wanandroid.http.HttpClient;
import com.able.wanandroid.http.RetrofitApi;
import com.able.wanandroid.http.RetrofitFactory;
import com.able.wanandroid.module.hotkey.repository.HotKeyDataSource;
import com.able.wanandroid.module.hotkey.repository.local.LocalHotKeyDataSource;

import java.util.List;

import retrofit2.Call;

public class RemoteHotKeyDataSource implements HotKeyDataSource {

    private static final RemoteHotKeyDataSource instance = new RemoteHotKeyDataSource();

    public RemoteHotKeyDataSource() {
    }

    public static RemoteHotKeyDataSource getInstance() {
        return instance;
    }

    private RetrofitApi api = RetrofitFactory.getInstance().create(RetrofitApi.class);

    @Override
    public LiveData<Resource<List<HotKeyBean>>> queryAll() {
        final MutableLiveData<Resource<List<HotKeyBean>>> liveData = new MutableLiveData<>();
        liveData.setValue(Resource.<List<HotKeyBean>>loading());
        api.getHotKey().enqueue(new ApiCallback<BaseResult<List<HotKeyBean>>>() {
            @Override
            public void onResponse(Call<BaseResult<List<HotKeyBean>>> call, BaseResult<List<HotKeyBean>> result) {
                liveData.setValue(Resource.response(result));
//                LocalHotKeyDataSource.getInstance().add(result.getData().get(0));
                LocalHotKeyDataSource.getInstance().addList(result.getData());
            }

            @Override
            public void onError(Call<BaseResult<List<HotKeyBean>>> call, Throwable t) {
                liveData.setValue(Resource.<List<HotKeyBean>>error(t));
            }
        });
        return liveData;
//        return HttpClient.getInstance().getHotKey();
    }
}
